



<!doctype html>
<html lang="en" class="no-js">
  <head>

<style>
  h1, h2, h3 { color: #04A9F4; }
  pre { color: black !important; }
</style>


      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta http-equiv="x-ua-compatible" content="ie=edge">

        <meta name="description" content="Open source package for Survival Analysis modeling">


        <link rel="canonical" href="https://www.pysurvival.io/miscellaneous/tips.html">


        <meta name="author" content="The PySurvival Team">


        <meta name="lang:clipboard.copy" content="Copy to clipboard">

        <meta name="lang:clipboard.copied" content="Copied to clipboard">

        <meta name="lang:search.language" content="en">

        <meta name="lang:search.pipeline.stopwords" content="True">

        <meta name="lang:search.pipeline.trimmer" content="True">

        <meta name="lang:search.result.none" content="No matching documents">

        <meta name="lang:search.result.one" content="1 matching document">

        <meta name="lang:search.result.other" content="# matching documents">

        <meta name="lang:search.tokenizer" content="[\s\-]+">

      <link rel="shortcut icon" href="../images/favicon.ico">
      <meta name="generator" content="mkdocs-1.0.4, mkdocs-material-4.0.2">



        <title>Tips for fitting models - PySurvival</title>



      <link rel="stylesheet" href="../assets/stylesheets/application.982221ab.css">

        <link rel="stylesheet" href="../assets/stylesheets/application-palette.224b79ff.css">




        <meta name="theme-color" content="#2196f3">



      <script src="../assets/javascripts/modernizr.1f0bcf2b.js"></script>



        <link href="https://fonts.gstatic.com" rel="preconnect" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=:300,400,400i,700|">
        <style>body,input{font-family:"","Helvetica Neue",Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"","Courier New",Courier,monospace}</style>


    <link rel="stylesheet" href="../assets/fonts/material-icons.css">






  </head>



    <body dir="ltr" data-md-color-primary="blue" data-md-color-accent="purple">

    <svg class="md-svg">
      <defs>


          <svg xmlns="http://www.w3.org/2000/svg" width="416" height="448"
    viewBox="0 0 416 448" id="__github">
  <path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19-18.125
        8.5-18.125-8.5-10.75-19-3.125-20.5 3.125-20.5 10.75-19 18.125-8.5
        18.125 8.5 10.75 19 3.125 20.5zM320 304q0 10-3.125 20.5t-10.75
        19-18.125 8.5-18.125-8.5-10.75-19-3.125-20.5 3.125-20.5 10.75-19
        18.125-8.5 18.125 8.5 10.75 19 3.125 20.5zM360
        304q0-30-17.25-51t-46.75-21q-10.25 0-48.75 5.25-17.75 2.75-39.25
        2.75t-39.25-2.75q-38-5.25-48.75-5.25-29.5 0-46.75 21t-17.25 51q0 22 8
        38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0
        37.25-1.75t35-7.375 30.5-15 20.25-25.75 8-38.375zM416 260q0 51.75-15.25
        82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5-41.75
        1.125q-19.5 0-35.5-0.75t-36.875-3.125-38.125-7.5-34.25-12.875-30.25-20.25-21.5-28.75q-15.5-30.75-15.5-82.75
        0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25
        30.875q36.75-8.75 77.25-8.75 37 0 70 8 26.25-20.5
        46.75-30.25t47.25-9.75q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34
        99.5z" />
</svg>

      </defs>
    </svg>
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" data-md-component="overlay" for="__drawer"></label>

      <a href="#tips-on-building-models" tabindex="1" class="md-skip">
        Skip to content
      </a>


      <header class="md-header" data-md-component="header">
  <nav class="md-header-nav md-grid">
    <div class="md-flex">
      <div class="md-flex__cell md-flex__cell--shrink">
        <a href="https://www.pysurvival.io/" title="PySurvival" class="md-header-nav__button md-logo">

            <img src="../images/logo.ico" width="24" height="24">

        </a>
      </div>
      <div class="md-flex__cell md-flex__cell--shrink">
        <label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
      </div>
      <div class="md-flex__cell md-flex__cell--stretch">
        <div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">

            <span class="md-header-nav__topic">
              PySurvival
            </span>
            <span class="md-header-nav__topic">
              Tips for fitting models
            </span>

        </div>
      </div>

              <!-- Global site tag (gtag.js) - Google Analytics -->
              <script async src="https://www.googletagmanager.com/gtag/js?id=UA-136790579-1"></script>
              <script>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());

                gtag('config', 'UA-136790579-1');
              </script>

      <div class="md-flex__cell md-flex__cell--shrink">

          <label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>

<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
      <label class="md-icon md-search__icon" for="__search"></label>
      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        &#xE5CD;
      </button>
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="result">
          <div class="md-search-result__meta">
            Type to start searching
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>

      </div>

        <div class="md-flex__cell md-flex__cell--shrink">
          <div class="md-header-nav__source">





<a href="https://github.com/square/pysurvival/" title="Go to repository" class="md-source" >

    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>

  <div class="md-source__repository">
    square/pysurvival
  </div>
</a>
          </div>
        </div>

    </div>
  </nav>
</header>

    <div class="md-container">




      <main class="md-main">
        <div class="md-main__inner md-grid" data-md-component="container">


              <div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    <nav class="md-nav md-nav--primary" data-md-level="0">
  <label class="md-nav__title md-nav__title--site" for="__drawer">
    <a href="https://www.pysurvival.io/" title="PySurvival" class="md-nav__button md-logo">

        <img src="../images/logo.ico" width="48" height="48">

    </a>
    PySurvival
  </label>

    <div class="md-nav__source">





<a href="https://github.com/square/pysurvival/" title="Go to repository" class="md-source" >

    <div class="md-source__icon">
      <svg viewBox="0 0 24 24" width="24" height="24">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>
    </div>

  <div class="md-source__repository">
    square/pysurvival
  </div>
</a>
    </div>

  <ul class="md-nav__list" data-md-scrollfix>






  <li class="md-nav__item">
    <a href="../index.html" title="Home" class="md-nav__link">
      Home
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../installation.html" title="Installation" class="md-nav__link">
      Installation
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../intro.html" title="Introduction to Survival Analysis" class="md-nav__link">
      Introduction to Survival Analysis
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../math.html" title="The math of Survival Analysis" class="md-nav__link">
      The math of Survival Analysis
    </a>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-5" type="checkbox" id="nav-5">

    <label class="md-nav__link" for="nav-5">
      Tutorials
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-5">
        Tutorials
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../tutorials/churn.html" title="Churn Prediction" class="md-nav__link">
      Churn Prediction
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../tutorials/credit_risk.html" title="Credit Risk" class="md-nav__link">
      Credit Risk
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../tutorials/employee_retention.html" title="Employee Retention" class="md-nav__link">
      Employee Retention
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../tutorials/maintenance.html" title="Predictive Maintenance" class="md-nav__link">
      Predictive Maintenance
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6" type="checkbox" id="nav-6">

    <label class="md-nav__link" for="nav-6">
      Models
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-6">
        Models
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-1" type="checkbox" id="nav-6-1">

    <label class="md-nav__link" for="nav-6-1">
      Cox Proportional Hazard
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-1">
        Cox Proportional Hazard
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/coxph.html" title="Standard CoxPH  (API)" class="md-nav__link">
      Standard CoxPH  (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/nonlinear_coxph.html" title="DeepSurv/Nonlinear CoxPH (API)" class="md-nav__link">
      DeepSurv/Nonlinear CoxPH (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/semi_parametric.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-2" type="checkbox" id="nav-6-2">

    <label class="md-nav__link" for="nav-6-2">
      Multi-Task Logistic Regression (MTLR)
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-2">
        Multi-Task Logistic Regression (MTLR)
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/linear_mtlr.html" title="Linear MTLR (API)" class="md-nav__link">
      Linear MTLR (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/neural_mtlr.html" title="Neural MTLR (API)" class="md-nav__link">
      Neural MTLR (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/mtlr_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-3" type="checkbox" id="nav-6-3">

    <label class="md-nav__link" for="nav-6-3">
      Non-Parametric
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-3">
        Non-Parametric
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/kaplan_meier.html" title="Kaplan Meier (API)" class="md-nav__link">
      Kaplan Meier (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/smooth_kaplan_meier.html" title="Smoothed Kaplan Meier (API)" class="md-nav__link">
      Smoothed Kaplan Meier (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/non_parametric.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-4" type="checkbox" id="nav-6-4">

    <label class="md-nav__link" for="nav-6-4">
      Parametric
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-4">
        Parametric
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/parametric.html" title="Parametric models (API)" class="md-nav__link">
      Parametric models (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/parametric_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-5" type="checkbox" id="nav-6-5">

    <label class="md-nav__link" for="nav-6-5">
      Simulation
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-5">
        Simulation
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/simulations.html" title="Simulation models (API)" class="md-nav__link">
      Simulation models (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/simulations_theory.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-6" type="checkbox" id="nav-6-6">

    <label class="md-nav__link" for="nav-6-6">
      Survival Forest
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-6">
        Survival Forest
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/conditional_survival_forest.html" title="Conditional Survival Forest (API)" class="md-nav__link">
      Conditional Survival Forest (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/extra_survival_trees.html" title="Extra Survival Trees (API)" class="md-nav__link">
      Extra Survival Trees (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/random_survival_forest.html" title="Random Survival Forest (API)" class="md-nav__link">
      Random Survival Forest (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/survival_forest.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-6-7" type="checkbox" id="nav-6-7">

    <label class="md-nav__link" for="nav-6-7">
      Survival SVM
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="2">
      <label class="md-nav__title" for="nav-6-7">
        Survival SVM
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../models/linear_svm.html" title="Linear Survival SVM (API)" class="md-nav__link">
      Linear Survival SVM (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/kernel_svm.html" title="Kernel Survival SVM (API)" class="md-nav__link">
      Kernel Survival SVM (API)
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../models/survival_svm.html" title="Theory" class="md-nav__link">
      Theory
    </a>
  </li>


      </ul>
    </nav>
  </li>


      </ul>
    </nav>
  </li>







  <li class="md-nav__item md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-7" type="checkbox" id="nav-7">

    <label class="md-nav__link" for="nav-7">
      Performance metrics
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-7">
        Performance metrics
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="../metrics/c_index.html" title="C-index" class="md-nav__link">
      C-index
    </a>
  </li>







  <li class="md-nav__item">
    <a href="../metrics/brier_score.html" title="Brier Score" class="md-nav__link">
      Brier Score
    </a>
  </li>


      </ul>
    </nav>
  </li>









  <li class="md-nav__item md-nav__item--active md-nav__item--nested">

      <input class="md-toggle md-nav__toggle" data-md-toggle="nav-8" type="checkbox" id="nav-8" checked>

    <label class="md-nav__link" for="nav-8">
      Miscellaneous
    </label>
    <nav class="md-nav" data-md-component="collapsible" data-md-level="1">
      <label class="md-nav__title" for="nav-8">
        Miscellaneous
      </label>
      <ul class="md-nav__list" data-md-scrollfix>







  <li class="md-nav__item">
    <a href="activation_functions.html" title="Activation Functions" class="md-nav__link">
      Activation Functions
    </a>
  </li>







  <li class="md-nav__item">
    <a href="save_load.html" title="Saving/Loading models" class="md-nav__link">
      Saving/Loading models
    </a>
  </li>









  <li class="md-nav__item md-nav__item--active">

    <input class="md-toggle md-nav__toggle" data-md-toggle="toc" type="checkbox" id="__toc">




      <label class="md-nav__link md-nav__link--active" for="__toc">
        Tips for fitting models
      </label>

    <a href="tips.html" title="Tips for fitting models" class="md-nav__link md-nav__link--active">
      Tips for fitting models
    </a>


<nav class="md-nav md-nav--secondary">





    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>

        <li class="md-nav__item">
  <a href="#reaching-complete-optimization" title="Reaching complete optimization" class="md-nav__link">
    Reaching complete optimization
  </a>

</li>

        <li class="md-nav__item">
  <a href="#actual-vs-predicted" title="Actual vs Predicted" class="md-nav__link">
    Actual vs Predicted
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#api" title="API" class="md-nav__link">
    API
  </a>

</li>

          <li class="md-nav__item">
  <a href="#example" title="Example" class="md-nav__link">
    Example
  </a>

</li>

      </ul>
    </nav>

</li>





    </ul>

</nav>

  </li>


      </ul>
    </nav>
  </li>


  </ul>
</nav>
                  </div>
                </div>
              </div>


              <div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">

<nav class="md-nav md-nav--secondary">





    <label class="md-nav__title" for="__toc">Table of contents</label>
    <ul class="md-nav__list" data-md-scrollfix>

        <li class="md-nav__item">
  <a href="#reaching-complete-optimization" title="Reaching complete optimization" class="md-nav__link">
    Reaching complete optimization
  </a>

</li>

        <li class="md-nav__item">
  <a href="#actual-vs-predicted" title="Actual vs Predicted" class="md-nav__link">
    Actual vs Predicted
  </a>

    <nav class="md-nav">
      <ul class="md-nav__list">

          <li class="md-nav__item">
  <a href="#api" title="API" class="md-nav__link">
    API
  </a>

</li>

          <li class="md-nav__item">
  <a href="#example" title="Example" class="md-nav__link">
    Example
  </a>

</li>

      </ul>
    </nav>

</li>





    </ul>

</nav>
                  </div>
                </div>
              </div>


          <div class="md-content">
            <article class="md-content__inner md-typeset">


                  <a href="https://github.com/square/pysurvival/edit/master/docs/miscellaneous/tips.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>


                <!-- Tips on building models -->

<style>
  h1, h2, h3, h4 { color: #04A9F4; }
</style>

<h1 id="tips-on-building-models">Tips on building models</h1>
<h2 id="reaching-complete-optimization">Reaching complete optimization</h2>
<p>It is advised to check the loss function values of the model once fitted to ensure that the best model parameters were computed. This can be done with the function <code>pysurvival.utils.display.display_loss_values</code>.</p>
<p>If the loss function reached a minimum, it will display a plateau after a certain number of epochs. If it is not the case the model will provide sub-optimal results. To solve this, it is advised to :</p>
<ul>
<li>either increase the learning rate <code>lr</code> </li>
<li>or increase the number of epochs <code>num_epochs</code>.</li>
</ul>
<p>Let's illustrate this with an example:</p>
<ul>
<li>
<p>Here we will build a model with a low learning rate; as a result the model will yield poor performances
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.models.semi_parametric</span> <span style="color: #008000; font-weight: bold">import</span> NonLinearCoxPHModel
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> display_loss_values
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.metrics</span> <span style="color: #008000; font-weight: bold">import</span> concordance_index
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.datasets</span> <span style="color: #008000; font-weight: bold">import</span> Dataset
<span style="color: #666666">%</span>pylab inline

<span style="color: #408080; font-style: italic"># Loading and splitting a simple example into train/test sets</span>
X_train, T_train, E_train, \
    X_test, T_test, E_test <span style="color: #666666">=</span> Dataset(<span style="color: #BA2121">&#39;simple_example&#39;</span>)<span style="color: #666666">.</span>load_train_test()

<span style="color: #408080; font-style: italic"># Let&#39;s build a Nonlinear CoxPH model</span>
structure <span style="color: #666666">=</span> [ {<span style="color: #BA2121">&#39;activation&#39;</span>: <span style="color: #BA2121">&#39;Atan&#39;</span>, <span style="color: #BA2121">&#39;num_units&#39;</span>: <span style="color: #666666">150</span>},  ]
nonlinear_coxph <span style="color: #666666">=</span> NonLinearCoxPHModel(structure<span style="color: #666666">=</span>structure)
nonlinear_coxph<span style="color: #666666">.</span>fit(X_train, T_train, E_train, lr<span style="color: #666666">=1e-6</span>, dropout<span style="color: #666666">=0.</span>)

<span style="color: #408080; font-style: italic"># We can now display the loss function values in respect to the epochs</span>
display_loss_values(nonlinear_coxph)
</pre></div>
<center><table class="image">
<caption align="bottom">Figure 1 - Loss values of a model with sub-optimal performances</caption>
<tr><td><center><img src="images/optim_bad.png" alt="PySurvival - Loss values of a model with poor performances" title="PySurvival - Loss values of a model with poor performances" width=100%, height=100%  /></center>
</td></tr>
</table>
</center>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Computing the c-index</span>
c_index <span style="color: #666666">=</span> concordance_index(nonlinear_coxph, X_test, T_test, E_test) <span style="color: #408080; font-style: italic">#0.57</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;C-index: {:.2f}&#39;</span><span style="color: #666666">.</span>format(c_index))
</pre></div>
The c-index is 0.57, it is very likely that the model will yield poor results.</p>
</li>
<li>
<p>By speeding the optimization, we managed to get a much better model. To achieve this, we can simply use a higher learning rate.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Let&#39;s rebuild the model with a bigger learning</span>
<span style="color: #408080; font-style: italic"># This will accelerate the optimization</span>
nonlinear_coxph<span style="color: #666666">.</span>fit(X_train, T_train, E_train, lr<span style="color: #666666">=1e-3</span>, dropout<span style="color: #666666">=0.</span>)

<span style="color: #408080; font-style: italic"># We can now display the loss function values in respect to the epochs</span>
display_loss_values(nonlinear_coxph)
</pre></div>
<center><table class="image">
<caption align="bottom">Figure 2 - Loss values of a model with better performances</caption>
<tr><td><center><img src="images/optim_good.png" alt="PySurvival - Loss values of a model with better performances" title="PySurvival - Loss values of a model with better performances" width=100%, height=100%  /></center>
</td></tr>
</table>
</center>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Computing the c-index</span>
c_index <span style="color: #666666">=</span> concordance_index(nonlinear_coxph, X_test, T_test, E_test) <span style="color: #408080; font-style: italic">#0.97</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;C-index: {:.2f}&#39;</span><span style="color: #666666">.</span>format(c_index))
</pre></div>
The c-index is 0.97, and we can see that the loss function has plateaued around epoch 400.</p>
</li>
</ul>
<hr />
<h2 id="actual-vs-predicted">Actual vs Predicted</h2>
<p>Once a model is built, it is always a good idea to compare the time series of the actual and predicted number of units that experienced the event at each time t. To do so, we compute the actual density/survival function of the data, which can be obtained using the <a href="../models/kaplan_meier.html">Kaplan-Meier estimator</a> and compare it to the average of all predicted density/survival functions.</p>
<p>The function <code>pysurvival.utils.metrics.compare_to_actual</code> can provide the comparison as well as the performance metrics between the two time series, such as:</p>
<ul>
<li>RMSE</li>
<li>Median Absolute Error</li>
<li>Mean Absolute Error</li>
</ul>
<p>The function <code>pysurvival.utils.display.compare_to_actual</code>  will also provide the charts.</p>
<h3 id="api">API</h3>
<div class="admonition abstract">
<p class="admonition-title"><code>compare_to_actual</code>  - Comparing the actual and predicted number of units at risk and units experiencing an event at each time t.</p>
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>compare_to_actual(model, X, T, E, times = None, is_at_risk = False,
        figure_size=(16, 6), metrics = [&#39;rmse&#39;, &#39;mean&#39;, &#39;median&#39;])
</pre></div>

<p><strong>Parameters:</strong></p>
<ul>
<li>
<p><code>model</code> : <strong>Pysurvival object</strong> --
    Pysurvival model</p>
</li>
<li>
<p><code>X</code> : <strong>array-like</strong> --
    input samples; where the rows correspond to an individual sample and the columns represent the features <em>(shape=[n_samples, n_features])</em>.</p>
</li>
<li>
<p><code>T</code> : <strong>array-like</strong> --
    target values describing the time when the event of interest or censoring
    occurred.</p>
</li>
<li>
<p><code>E</code> : <strong>array-like</strong> --
    values that indicate if the event of interest occurred i.e.: E[i]=1
    corresponds to an event, and E[i] = 0 means censoring, for all i.</p>
</li>
<li>
<p><code>times</code>: <strong>array-like</strong> -- (default=None)
    A vector of time-points.</p>
</li>
<li>
<p><code>is_at_risk</code>: <strong>bool</strong> -- (default=True)
    Whether the function returns Expected number of units at risk
    or the Expected number of units experiencing the events.</p>
</li>
<li>
<p><code>figure_size</code>: <strong>tuple of double</strong> -- (default= (16, 6))
    width, height in inches representing the size of the chart </p>
</li>
<li>
<p><code>metrics</code>: <strong>str</strong> or <strong>list of str</strong> (default='all')
    Indicates the performance metrics to compute:</p>
<ul>
<li>if None, then no metric is computed</li>
<li>if str, then the metric is computed</li>
<li>if list of str, then all the given metrics are computed</li>
</ul>
<p>The available metrics are:</p>
<ul>
<li>RMSE: root mean squared error</li>
<li>Mean Abs Error: mean absolute error</li>
<li>Median Abs Error: median absolute error</li>
</ul>
</li>
</ul>
<p><strong>Returns:</strong></p>
<ul>
<li>results: <strong>dict</strong> --
    dictionary containing the performance metrics</li>
</ul>
</div>
<h3 id="example">Example</h3>
<p>All the tutorials display an instance of how to use the function. Let's take the example of the <a href="../tutorials/credit_risk.html#71-overall-predictions">Credit Risk tutorial</a>:</p>
<ul>
<li>the time series of the actual and predicted number of loans that were fully repaid, for each time t.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysurvival.utils.display</span> <span style="color: #008000; font-weight: bold">import</span> compare_to_actual
results <span style="color: #666666">=</span> compare_to_actual(neural_mtlr, X_test, T_test, E_test,
                            is_at_risk <span style="color: #666666">=</span> <span style="color: #008000">False</span>,  figure_size<span style="color: #666666">=</span>(<span style="color: #666666">16</span>, <span style="color: #666666">6</span>),
                            metrics <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;rmse&#39;</span>, <span style="color: #BA2121">&#39;mean&#39;</span>, <span style="color: #BA2121">&#39;median&#39;</span>])
</pre></div></li>
</ul>
<p><center><img src="../tutorials/images/credit_global_pred_1.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were fully repaid" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were fully repaid" width=100%, height=100%  /></center>
<center>Figure 3 - Actual vs Predicted - Number of loans that were fully repaid</center></p>
<ul>
<li>the time series of the actual and predicted number of loans that were still active, for each time t.
<div class="codehilite" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>results <span style="color: #666666">=</span> compare_to_actual(neural_mtlr, X_test, T_test, E_test,
                            is_at_risk <span style="color: #666666">=</span> <span style="color: #008000">True</span>,  figure_size<span style="color: #666666">=</span>(<span style="color: #666666">16</span>, <span style="color: #666666">6</span>),
                            metrics <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;rmse&#39;</span>, <span style="color: #BA2121">&#39;mean&#39;</span>, <span style="color: #BA2121">&#39;median&#39;</span>])
</pre></div></li>
</ul>
<p><center><img src="../tutorials/images/credit_global_pred_2.png" alt="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were still active" title="PySurvival - Credit Risk Tutorial - Neural MTLR - Number of loans that were still active" width=100%, height=100%  /></center>
<center>Figure 4 - Actual vs Predicted - Number of loans that were still active</center></p>









            </article>
          </div>
        </div>
      </main>


<footer class="md-footer">

  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-footer-copyright">

          <div class="md-footer-copyright__highlight">
            Copyright &copy; 2019 Square Inc.
          </div>



        brought to you by
        <a href="https://squareup.com/us">
        <img src="images/Square_logo.png" alt="Square Logo" title="Square Logo" width=20%, height=20% align="center"  /></a>


      </div>

  <div class="md-footer-social">
    <link rel="stylesheet" href="../assets/fonts/font-awesome.css">

      <a href="https://pysurvival.io/" class="md-footer-social__link fa fa-home"></a>

      <a href="https://github.com/square/pysurvival" class="md-footer-social__link fa fa-github"></a>

  </div>

    </div>
  </div>
</footer>

    </div>

      <script src="../assets/javascripts/application.d9aa80ab.js"></script>

      <script>app.initialize({version:"1.0.4",url:{base:".."}})</script>

        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>

        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


  </body>
</html>
